/*!
 * \file frct_hash_output.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_hash.h"
#include "frct_mng.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

void __hash_node_free(Frlist_node *p_st_node);

extern Frhash_header        *g_p_st_hash_ct;

static void __case1(void)
{
    char *p_result = NULL;

    g_p_st_hash_ct = NULL;

    frct_hash_output(&p_result);

    CU_ASSERT_PTR_NULL(p_result);

    frfree(p_result);

    return;
}
//
//static void __case2(void)
//{
//    Frlist_node *p_st_node = NULL;
//    Conntrack *p_st_ct = NULL;
//    Conntrack *p_st_ct1 = NULL;
//    char *p_key = NULL;
//    int i_key_len = 15;
//    unsigned int ui_table_size = 65535;
//    char *p_result = NULL;
//
//    g_p_st_hash_ct = NULL;
//    frct_hash_init(ui_table_size);
//
//    p_key = (char *)frmalloc(i_key_len);
//
//    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
//    memset(p_st_ct,'\0',sizeof(Conntrack));
//    p_st_ct->src_ip = iptools_parse_ipaddr((char *)"1192.168.1.1");
//    p_st_ct->src_port = 80;
//    p_st_ct->dst_ip = iptools_parse_ipaddr((char *)"1192.168.0.1");
//    p_st_ct->dst_port = 22;
//    p_st_ct->ip_proto = 6;
//
//    i_key_len = frct_hash_key(p_st_ct->ip_proto, p_st_ct->src_ip, p_st_ct->src_port
//            , p_st_ct->dst_ip, p_st_ct->dst_port, p_key, i_key_len);
//    p_st_node = frct_hash_add(p_st_ct, p_key, i_key_len);
//
//    frct_hash_output(&p_result);
//
//    CU_ASSERT_PTR_NOT_NULL(p_result);
//    printf("\n%s\n",p_result);
//    CU_ASSERT_STRING_NOT_EQUAL(p_result,"\0");
////    CU_ASSERT_STRING_EQUAL(p_result,"6(tcp)192.168.1.2:77->192.168.0.2:78 refcnt=0 ct_direct=unknown master=null self=0x612660 [l7_analyzer:]\n");
//    frfree(p_st_ct);
//    frfree(p_key);
//    frfree(p_result);
//    frhash2_free2(g_p_st_hash_ct,__hash_node_free);
//    g_p_st_hash_ct = NULL;
//    return;
//}

static void __case3(void)
{
    unsigned int ui_table_size = 65535;
    char *p_result = NULL;

    g_p_st_hash_ct = NULL;
    frct_hash_init(ui_table_size);

    frct_hash_output(&p_result);

    CU_ASSERT_STRING_EQUAL(p_result,"\0");

    frhash2_free2(g_p_st_hash_ct,__hash_node_free);
    g_p_st_hash_ct = NULL;
    frfree(p_result);
    return;
}
//
//static void __case4(void)
//{
//    Frlist_node *p_st_node = NULL;
//    Conntrack *p_st_ct = NULL;
//    Conntrack *p_st_ct1 = NULL;
//    char *p_key = NULL;
//    char *p_key1 = NULL;
//    int i_key_len = 15;
//    unsigned int ui_table_size = 65535;
//    char *p_result = NULL;
//
//    g_p_st_hash_ct = NULL;
//    frct_hash_init(ui_table_size);
//
//    p_key = (char *)frmalloc(i_key_len);
//
//    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
//    memset(p_st_ct,'\0',sizeof(Conntrack));
//    p_st_ct->src_ip = iptools_parse_ipaddr((char *)"1192.168.1.1");
//    p_st_ct->src_port = 80;
//    p_st_ct->dst_ip = iptools_parse_ipaddr((char *)"1192.168.0.1");
//    p_st_ct->dst_port = 22;
//    p_st_ct->ip_proto = 6;
//
//    i_key_len = frct_hash_key(p_st_ct->ip_proto, p_st_ct->src_ip, p_st_ct->src_port
//            , p_st_ct->dst_ip, p_st_ct->dst_port, p_key, i_key_len);
//    p_st_node = frct_hash_add(p_st_ct, p_key, i_key_len);
//
//    p_st_ct1 = (Conntrack *)frmalloc(sizeof(Conntrack));
//    memset(p_st_ct1,'\0',sizeof(Conntrack));
//    p_st_ct1->src_ip = iptools_parse_ipaddr((char *)"1192.168.1.2");
//    p_st_ct1->src_port = 77;
//    p_st_ct1->dst_ip = iptools_parse_ipaddr((char *)"1192.168.0.2");
//    p_st_ct1->dst_port = 78;
//    p_st_ct1->ip_proto = 6;
//
//    i_key_len = 15;
//    p_key1 = (char *)frmalloc(i_key_len);
//
//    i_key_len = frct_hash_key(p_st_ct1->ip_proto, p_st_ct1->src_ip, p_st_ct1->src_port
//            , p_st_ct1->dst_ip, p_st_ct1->dst_port, p_key1, i_key_len);
//    p_st_node = frct_hash_add(p_st_ct1, p_key1, i_key_len);
//
//    frct_hash_output(&p_result);
//
//    //  CU_ASSERT_STRING_EQUAL(p_result,"6(tcp)192.168.1.1:80->192.168.0.1:22 refcnt=0 ct_direct=unknown master=null self=0x612660 [l7_analyzer:]\n");
//    CU_ASSERT_STRING_NOT_EQUAL(p_result,"\0");
//    printf("\n%s\n",p_result);
//
//    frfree(p_st_ct);
//    frfree(p_st_ct1);
//    frfree(p_key);
//    frfree(p_key1);
//    frfree(p_result);
//    frhash2_free2(g_p_st_hash_ct,__hash_node_free);
//    g_p_st_hash_ct = NULL;
//    return;
//}

int add_suite_frct_hash_output(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frct_hash frct_hash_output()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
//    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case2", __case3);
//    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
